package com.gcd.gym.api;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gcd.gym.model.Coach;
import com.gcd.gym.service.CoachService;
import com.gcd.gym.service.UploadService;
import com.gcd.gym.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping(value = "/api/v1/coach",produces = MediaType.APPLICATION_JSON_VALUE)
public class CoachApi {
    private CoachService coachService;
    private UploadService uploadService;

    @Autowired
    public void setUploadService(UploadService uploadService) {
        this.uploadService = uploadService;
    }

    @Autowired
    public void setCoachService(CoachService coachService) {
        this.coachService = coachService;
    }


/*    查询全部
    按照姓名，手机，微信查询*/
    @GetMapping
    public ResponseEntity<JsonResult<?>> findAllCoach(@RequestParam(defaultValue = "1") @Validated int pageNo,
                                                          @RequestParam(defaultValue = "5") @Validated int pageSize,
                                                          Coach coach) {
        Page<Coach> page=new Page<>(pageNo,pageSize);
        page=coachService.findAllCoach(page,coach);
        return ResponseEntity.ok(JsonResult.success(page));
    }

    @DeleteMapping
    public ResponseEntity<JsonResult<?>> deleteCoachById(@RequestBody @Validated
                                                             Integer[] ids) {
        List<Integer> coachId= Arrays.asList(ids);
        int count=coachService.deleteCoachById(coachId);
        if (count==0) {
            return ResponseEntity.ok(JsonResult.fail("教练删除失败！"));
        } else {
            return ResponseEntity.ok(JsonResult.success(count));
        }
    }


    @PostMapping
    public ResponseEntity<JsonResult<?>> addCoach(@RequestBody @Validated  Coach coach) {
        boolean flag= coachService.addCoach(coach);
        if(flag) {
            return ResponseEntity.ok(JsonResult.success(flag));
        } else {
            return ResponseEntity.ok(JsonResult.fail("添加教练失败"));
        }
    }


    @PutMapping
    public ResponseEntity<JsonResult<?>> updateCoach(@RequestBody @Validated Coach coach) {
        boolean success=coachService.updateCoach(coach);
        if(success) {
            return ResponseEntity.ok(JsonResult.success(coach));
        } else {
            return ResponseEntity.ok(JsonResult.fail("修改失败"));
        }
    }

    @PostMapping(value = "/avatar",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<JsonResult<?>> uploadCoachAvatar(MultipartFile file) {
        String path=uploadService.uploadImage(file,"coach_avatat");
        return ResponseEntity.ok(JsonResult.success(path));
    }
}
